package com.ys.erp.stage_mall_service.service;

import com.alibaba.fastjson2.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ys.erp.common.enums.EnableEnum;
import com.ys.erp.common.util.MoneyUtil;
import com.ys.erp.common.util.YsThread;
import com.ys.erp.entity.data.bean.*;
import com.ys.erp.entity.service.*;
import com.ys.erp.framework.bean.dto.StageEmpDTO;
import com.ys.erp.framework.exception.YsException;
import com.ys.erp.stage_mall_service.bean.condition.KingKongConfigCondition;
import com.ys.erp.stage_mall_service.bean.vo.KingKongConfigInfoVO;
import com.ys.erp.stage_mall_service.bean.vo.KingKongConfigVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author minamiyama
 * @date 2025/5/16 10:53
 */
@RequiredArgsConstructor
@Service
@Log4j
public class KingKongConfigService {

    private final IKingKongConfigService iKingKongConfigService;
    private final IGoodsService iGoodsService;
    private final IGoodsBrandService iGoodsBrandService;
    private final IMallKingKongBrandService iMallKingKongBrandService;
    private final IMallHomeGoodsService iMallHomeGoodsService;
    private final IMallHomeGoodsCateService iMallHomeGoodsCateService;
    private final IMallHomeKingKongDisAreaService iMallHomeKingKongDisAreaService;

    /**
     * 添加KingKong配置信息
     *
     * @param con    KingKong配置条件，指定了添加配置的具体条件
     * @param empDTO 包含员工信息的数据传输对象，用于关联配置与员工
     */
    @Transactional(rollbackFor = Exception.class)
    public void addKingKongConfig(KingKongConfigCondition.AddOrUpdateKingKongConfigCon con, StageEmpDTO empDTO) {
        YsThread.execute(() -> {

        });
        if (Objects.isNull(con.getId())) {
            MallKingKongConfig mallKingKongConfig = new MallKingKongConfig();
            BeanUtils.copyProperties(con, mallKingKongConfig);
            mallKingKongConfig.setEmpId(empDTO.getId());
            mallKingKongConfig.setEmpPhone(empDTO.getPhone());
            mallKingKongConfig.setEmpName(empDTO.getRealName());
            mallKingKongConfig.setStatus(EnableEnum.NO.getEnable());
            mallKingKongConfig.setNo("king_kong" + UUID.randomUUID());
            mallKingKongConfig.setSort(con.getSort());
            iKingKongConfigService.save(mallKingKongConfig);
            byKingKongType(con, mallKingKongConfig);
            return;
        }
        MallKingKongConfig kingKong = iKingKongConfigService.getById(con.getId());
        if (Objects.isNull(kingKong)) {
            throw new YsException(-1, "源数据不存在，无法修改");
        }
        if (kingKong.getType().equals(MallKingKongConfig.Enums.BIG_BRAND_PURCHASE.name())) {
            LambdaQueryWrapper<MallKingKongBrand> kingKongBrandQw = new LambdaQueryWrapper<>();
            kingKongBrandQw.eq(MallKingKongBrand::getKingKongId, kingKong.getId());
            iMallKingKongBrandService.remove(kingKongBrandQw);
        }
        if (kingKong.getType().equals(MallKingKongConfig.Enums.CASH_ONLY.name())) {
            LambdaQueryWrapper<MallHomeGoodsCate> mallHomeGoodsCateQw = new LambdaQueryWrapper<>();
            mallHomeGoodsCateQw.eq(MallHomeGoodsCate::getGroupNo, kingKong.getNo());
            iMallHomeGoodsCateService.remove(mallHomeGoodsCateQw);
            LambdaQueryWrapper<MallHomeKingKongDisArea> disAreaQw = new LambdaQueryWrapper<>();
            disAreaQw.eq(MallHomeKingKongDisArea::getGroupNo, kingKong.getNo());
            iMallHomeKingKongDisAreaService.remove(disAreaQw);
            QueryWrapper<MallHomeGoods> mallHomeGoodsQueryWrapper = new QueryWrapper<>();
            mallHomeGoodsQueryWrapper.lambda().eq(MallHomeGoods::getHomeGroupNo, kingKong.getNo());
            iMallHomeGoodsService.remove(mallHomeGoodsQueryWrapper);
        }
        BeanUtils.copyProperties(con, kingKong);
        byKingKongType(con, kingKong);
        kingKong.setEmpId(empDTO.getId());
        kingKong.setEmpPhone(empDTO.getPhone());
        kingKong.setEmpName(empDTO.getRealName());

        iKingKongConfigService.updateById(kingKong);

    }

    /**
     * 获取金刚配置信息列表
     * 本方法通过异步处理方式，分别获取大品牌抢购、现金专区和品牌大厅的商品数量，并将这些数量信息
     * 以及其他配置信息封装到KingKongConfigVO对象中，最终返回一个包含所有配置信息的列表
     *
     * @return 包含金刚配置信息的列表
     */
    @Transactional(rollbackFor = Exception.class)
    public List<KingKongConfigVO> getKingKongConfig() {
        // 获取所有的金刚配置信息
        List<MallKingKongConfig> configList = iKingKongConfigService.list(new LambdaQueryWrapper<MallKingKongConfig>().orderByAsc(MallKingKongConfig::getSort));
        ArrayList<KingKongConfigVO> results = new ArrayList<>();

        // 异步获取大品牌抢购商品数量
        CompletableFuture<Map<Long, Integer>> bigBrandGoodsNumFuture = YsThread.execute(() -> {
            List<MallKingKongConfig> bigBrandList = configList.stream().filter(mallKingKongConfig ->
                    MallKingKongConfig.Enums.BIG_BRAND_PURCHASE.name().equals(mallKingKongConfig.getType())).toList();

            if (CollectionUtils.isEmpty(bigBrandList)) {
                return new HashMap<>();
            }
            List<Long> bigBrandIdList = bigBrandList.stream().map(MallKingKongConfig::getId).toList();
            QueryWrapper<MallKingKongBrand> brandQw = new QueryWrapper<>();
            brandQw.lambda().in(MallKingKongBrand::getKingKongId, bigBrandIdList);
            Map<Long, List<MallKingKongBrand>> brandMap = iMallKingKongBrandService.list(brandQw)
                    .stream().collect(Collectors.groupingBy(MallKingKongBrand::getKingKongId));
            HashMap<Long, Integer> result = new HashMap<>();
            brandMap.forEach((kingKongId, brandList) -> {
                List<Long> brandIdList = brandList.stream().map(MallKingKongBrand::getBrandId).toList();
                if (CollectionUtils.isEmpty(brandIdList)) {
                    result.put(kingKongId, 0);
                } else {
                    Integer brandGoodsNum = getBrandGoodsNum(brandIdList);
                    result.put(kingKongId, brandGoodsNum);
                }

            });
            return result;
        });

        // 异步获取现金专区商品数量
        CompletableFuture<Integer> cashOnlyGoodsNumFuture = YsThread.execute(() -> {
            MallKingKongConfig cashOnly = configList.stream().filter(mallKingKongConfig -> MallKingKongConfig.Enums.CASH_ONLY.name().equals(mallKingKongConfig.getType())).findAny().orElse(null);
            if (Objects.isNull(cashOnly)) {
                return 0;
            }
            QueryWrapper<MallHomeGoodsCate> mallHomeGoodsCateQw = new QueryWrapper<>();
            mallHomeGoodsCateQw.lambda().eq(MallHomeGoodsCate::getGroupNo, cashOnly.getNo());
            List<String> cateNoList = iMallHomeGoodsCateService.list(mallHomeGoodsCateQw).stream().map(MallHomeGoodsCate::getNo).toList();
            QueryWrapper<MallHomeKingKongDisArea> disAreaQw = new QueryWrapper<>();
            disAreaQw.lambda().eq(MallHomeKingKongDisArea::getGroupNo, cashOnly.getNo());
            List<MallHomeKingKongDisArea> disAreaList = iMallHomeKingKongDisAreaService.list(disAreaQw);
            List<String> disAreaNoList = disAreaList.stream().map(MallHomeKingKongDisArea::getNo).toList();
            if (CollectionUtils.isEmpty(disAreaNoList)) {
                return 0;
            }
            QueryWrapper<MallHomeGoods> homeGoodsQw = new QueryWrapper<>();
            homeGoodsQw.lambda().in(MallHomeGoods::getDisAreaNo, disAreaNoList);
            if (CollectionUtils.isNotEmpty(cateNoList)) {
                homeGoodsQw.lambda().in(MallHomeGoods::getMenuNo, cateNoList);
            }
            List<MallHomeGoods> homeGoodsList = iMallHomeGoodsService.list(homeGoodsQw);
            return homeGoodsList.size();
        });

        // 异步获取品牌大厅商品数量
        CompletableFuture<Integer> brandHallFuture = YsThread.execute(() -> {
            List<Long> brandIdList = iGoodsBrandService.list().stream().map(GoodsBrand::getId).toList();
            return getBrandGoodsNum(brandIdList);
        });

        // 等待所有异步任务完成并获取结果
        Map<Long, Integer> bigBrandGoodsNum = bigBrandGoodsNumFuture.join();
        Integer cashOnlyGoodsNum = cashOnlyGoodsNumFuture.join();
        Integer brandHall = brandHallFuture.join();

        // 遍历配置列表，封装VO对象，并设置商品数量信息
        for (MallKingKongConfig mallKingKongConfig : configList) {
            KingKongConfigVO vo = new KingKongConfigVO();
            vo.setId(mallKingKongConfig.getId());
            vo.setSort(mallKingKongConfig.getSort());
            vo.setTitle(mallKingKongConfig.getTitle());
            vo.setSubTitle(mallKingKongConfig.getSubTitle());
            vo.setIcon(mallKingKongConfig.getIcon());
            vo.setType(MallKingKongConfig.ENUM_MAP.get(mallKingKongConfig.getType()));
            if (mallKingKongConfig.getType().equals(MallKingKongConfig.Enums.BIG_BRAND_PURCHASE.name())) {
                vo.setLineGoodsNum(bigBrandGoodsNum.get(mallKingKongConfig.getId()));
            }
            if (mallKingKongConfig.getType().equals(MallKingKongConfig.Enums.CASH_ONLY.name())) {
                vo.setLineGoodsNum(cashOnlyGoodsNum);
            }
            if (mallKingKongConfig.getType().equals(MallKingKongConfig.Enums.BRAND_HALL.name())) {
                vo.setLineGoodsNum(brandHall);
            }
            vo.setOperateEmp(mallKingKongConfig.getEmpName() + " " + mallKingKongConfig.getEmpPhone());
            vo.setOperateTime(mallKingKongConfig.getUpdateTime());
            vo.setStatus(mallKingKongConfig.getStatus());
            results.add(vo);
        }

        // 返回封装好的配置信息列表
        return results;
    }

    @Transactional(rollbackFor = Exception.class)
    public void updateKingKongConfigStatus(Long id) {
        MallKingKongConfig kingKong = iKingKongConfigService.getById(id);
        if (Objects.isNull(kingKong)) {
            throw new YsException(-1, "该配置不存在");
        }
        if (kingKong.getStatus().equals(EnableEnum.NO.getEnable())) {
            QueryWrapper<MallKingKongConfig> kingKongConfigQw = new QueryWrapper<>();
            kingKongConfigQw.lambda().eq(MallKingKongConfig::getStatus, EnableEnum.YES.getEnable());
            kingKongConfigQw.lambda().eq(MallKingKongConfig::getType, kingKong.getType());
            kingKongConfigQw.lambda().ne(MallKingKongConfig::getId, kingKong.getId());
            List<MallKingKongConfig> kingKongList = iKingKongConfigService.list(kingKongConfigQw);
            if (CollectionUtils.isNotEmpty(kingKongList)) {
                throw new YsException(-1, "目前已有同类型内容启用中，请先禁用后再启用");
            }
        }
        kingKong.setStatus(kingKong.getStatus() == EnableEnum.YES.getEnable() ? EnableEnum.NO.getEnable() : EnableEnum.YES.getEnable());
        iKingKongConfigService.updateById(kingKong);
        YsThread.execute(() -> {

        });
    }

    /**
     * 删除金刚区配置
     *
     * @param id 金刚区配置的ID
     *           该方法异步执行删除操作，首先根据ID获取金刚区配置对象，然后检查其存在性和启用状态
     *           如果配置不存在，则抛出异常提示配置不存在；如果配置已启用，则抛出异常提示不支持删除
     *           随后，根据配置类型清理相关关联数据，最后删除配置本身
     */
    @Transactional(rollbackFor = Exception.class)
    public void deleteKingKongConfig(Long id) {
        // 根据ID获取金刚区配置对象
        MallKingKongConfig kingKong = iKingKongConfigService.getById(id);

        // 检查配置是否存在
        if (Objects.isNull(kingKong)) {
            throw new YsException(-1, "该配置不存在");
        }

        // 检查配置是否已启用，如果启用则不支持删除
        if (kingKong.getStatus().equals(EnableEnum.YES.getEnable())) {
            throw new YsException(-1, "当前内容启用中，不支持删除");
        }

        // 根据配置类型清理相关关联数据
        if (kingKong.getType().equals(MallKingKongConfig.Enums.BIG_BRAND_PURCHASE.name())) {
            // 删除大品牌抢购类型的关联品牌数据
            QueryWrapper<MallKingKongBrand> kingKongBrandQw = new QueryWrapper<>();
            kingKongBrandQw.lambda().eq(MallKingKongBrand::getKingKongId, kingKong.getId());
            iMallKingKongBrandService.remove(kingKongBrandQw);
        }
        if (kingKong.getType().equals(MallKingKongConfig.Enums.CASH_ONLY.name())) {
            // 删除现金专区类型的关联分类和区域数据，以及相关商品数据
            QueryWrapper<MallHomeGoodsCate> goodsCateQw = new QueryWrapper<>();
            goodsCateQw.lambda().eq(MallHomeGoodsCate::getGroupNo, kingKong.getNo());
            List<MallHomeGoodsCate> cateList = iMallHomeGoodsCateService.list(goodsCateQw);
            List<String> cateNoList = cateList.stream().map(MallHomeGoodsCate::getNo).toList();
            QueryWrapper<MallHomeKingKongDisArea> disAreaQw = new QueryWrapper<>();
            disAreaQw.lambda().eq(MallHomeKingKongDisArea::getGroupNo, kingKong.getNo());
            List<MallHomeKingKongDisArea> disAreaList = iMallHomeKingKongDisAreaService.list(disAreaQw);
            List<String> disAreaNoList = disAreaList.stream().map(MallHomeKingKongDisArea::getNo).toList();
            if (CollectionUtils.isNotEmpty(disAreaList)) {
                QueryWrapper<MallHomeGoods> goodsQw = new QueryWrapper<>();
                goodsQw.lambda().in(MallHomeGoods::getDisAreaNo, disAreaNoList);
                List<Long> goodsList = iMallHomeGoodsService.list(goodsQw).stream().map(MallHomeGoods::getId).toList();
                iMallHomeGoodsService.removeBatchByIds(goodsList);
            }
            iMallHomeGoodsCateService.remove(goodsCateQw);
            iMallHomeKingKongDisAreaService.remove(disAreaQw);
        }

        // 删除金刚区配置本身
        iKingKongConfigService.removeById(id);
        YsThread.execute(() -> {

        });
    }

    /**
     * 获取金刚配置信息
     *
     * @param id 金刚配置的ID
     * @return 金刚配置信息的视图对象
     * @throws YsException 如果内容不存在或数据错误
     */
    public KingKongConfigInfoVO getKingKongConfigInfo(Long id) {
        // 根据ID获取金刚配置
        MallKingKongConfig kingKong = iKingKongConfigService.getById(id);
        // 如果配置不存在，抛出异常
        if (Objects.isNull(kingKong)) {
            throw new YsException(-1, "内容不存在");
        }

        // 创建并填充金刚配置信息视图对象
        KingKongConfigInfoVO vo = new KingKongConfigInfoVO()
                .setId(kingKong.getId())
                .setSort(kingKong.getSort())
                .setTitle(kingKong.getTitle())
                .setSubTitle(kingKong.getSubTitle())
                .setIcon(kingKong.getIcon())
                .setType(kingKong.getType())
                .setBackgroundImage(kingKong.getBackgroundImage());

        // 获取商品信息JSON字符串
        //String goodsInfoJson = kingKong.getGoodsInfoJson();
        // 如果类型为空，抛出数据错误异常
        if (StringUtils.isBlank(kingKong.getType())) {
            throw new YsException(-1, "数据错误！！！！！！");
        }

        // 处理大品牌抢购类型
        if (kingKong.getType().equals(MallKingKongConfig.Enums.BIG_BRAND_PURCHASE.name())) {
            QueryWrapper<MallKingKongBrand> kingKongBrandQw = new QueryWrapper<>();
            kingKongBrandQw.lambda().eq(MallKingKongBrand::getKingKongId, kingKong.getId());
            kingKongBrandQw.lambda().orderByAsc(MallKingKongBrand::getId);
            List<Long> brandIdList = iMallKingKongBrandService.list(kingKongBrandQw).stream().map(MallKingKongBrand::getBrandId).toList();
            if (CollectionUtils.isEmpty(brandIdList)) {
                vo.setGoodsBrandList(new ArrayList<>());
                return vo;
            }
            // 查询品牌信息并设置到视图对象
            QueryWrapper<GoodsBrand> brandQw = new QueryWrapper<>();
            brandQw.lambda().in(GoodsBrand::getId, brandIdList);
            List<GoodsBrand> brandList = iGoodsBrandService.list(brandQw);
            Map<Long, GoodsBrand> brandMap = brandList.stream().collect(Collectors.toMap(GoodsBrand::getId, Function.identity()));
            ArrayList<KingKongConfigInfoVO.GoodsBrandVO> goodsBrandVos = new ArrayList<>();
            for (Long l : brandIdList) {
                GoodsBrand goodsBrand = brandMap.get(l);
                if (Objects.isNull(goodsBrand)) {
                    continue;
                }
                KingKongConfigInfoVO.GoodsBrandVO goodsBrandVO = new KingKongConfigInfoVO.GoodsBrandVO();
                goodsBrandVO.setId(goodsBrand.getId());
                goodsBrandVO.setName(goodsBrand.getBrandName());
                goodsBrandVos.add(goodsBrandVO);
            }
            vo.setGoodsBrandList(goodsBrandVos);
        }

        // 处理仅现金类型
        if (kingKong.getType().equals(MallKingKongConfig.Enums.CASH_ONLY.name())) {
            // 查询商品分类信息
            QueryWrapper<MallHomeGoodsCate> goodsCateQw = new QueryWrapper<>();
            goodsCateQw.lambda().eq(MallHomeGoodsCate::getGroupNo, kingKong.getNo());
            List<MallHomeGoodsCate> cateList = iMallHomeGoodsCateService.list(goodsCateQw);
            List<String> cateNoList = cateList.stream().map(MallHomeGoodsCate::getNo).toList();
            ArrayList<KingKongConfigInfoVO.GoodsMenu> goodsMenus = getGoodsMenus(cateList);
            vo.setGoodsMenus(goodsMenus);

            // 创建分类ID与名称的映射
            Map<String, String> cateMap = cateList.stream()
                    .collect(Collectors.toMap(MallHomeGoodsCate::getNo, MallHomeGoodsCate::getMenuName));

            // 查询分布区域信息
            QueryWrapper<MallHomeKingKongDisArea> disAreaQw = new QueryWrapper<>();
            disAreaQw.lambda().eq(MallHomeKingKongDisArea::getGroupNo, kingKong.getNo());
            List<MallHomeKingKongDisArea> disAreaList = iMallHomeKingKongDisAreaService.list(disAreaQw);
            List<String> disAreaNoList = disAreaList.stream().map(MallHomeKingKongDisArea::getNo).toList();

            // 查询商品信息
            QueryWrapper<MallHomeGoods> goodsQw = new QueryWrapper<>();
            if (CollectionUtils.isEmpty(disAreaNoList)) {
                vo.setDisAreas(new ArrayList<>());
                return vo;
            }
            goodsQw.lambda().in(MallHomeGoods::getDisAreaNo, disAreaNoList);
            List<MallHomeGoods> goodsList = iMallHomeGoodsService.list(goodsQw);

            // 将商品信息按分布区域ID分组
            Map<String, List<MallHomeGoods>> goodsListMap = goodsList.stream().collect(Collectors.groupingBy(MallHomeGoods::getDisAreaNo));
            List<KingKongConfigInfoVO.GoodsTableVO> goodsTableVOList = new ArrayList<>();
            for (MallHomeKingKongDisArea kingKongDisArea : disAreaList) {
                // 如果区域名称为空，跳过当前循环
                if (StringUtils.isBlank(kingKongDisArea.getAreaName())) {
                    continue;
                }
                KingKongConfigInfoVO.GoodsTableVO goodsTableVO = new KingKongConfigInfoVO.GoodsTableVO();
                goodsTableVO.setName(kingKongDisArea.getAreaName());

                List<MallHomeGoods> goods = goodsListMap.get(kingKongDisArea.getNo());
                // 如果当前区域的商品列表为空，设置空列表并添加到视图对象
                if (CollectionUtils.isEmpty(goods)) {
                    goodsTableVO.setGoodsList(Collections.emptyList());
                    goodsTableVOList.add(goodsTableVO);
                    continue;
                }

                // 创建并填充商品信息视图对象
                ArrayList<KingKongConfigInfoVO.GoodsInfoVO> goodsInfoVoS = new ArrayList<>();
                for (MallHomeGoods mallHomeGoods : goods) {
                    KingKongConfigInfoVO.GoodsInfoVO goodsInfoVO = new KingKongConfigInfoVO.GoodsInfoVO();
                    BeanUtils.copyProperties(mallHomeGoods, goodsInfoVO);
                    goodsInfoVO.setCateName("全部".equals(cateMap.get(mallHomeGoods.getMenuNo())) ? null :
                            cateMap.get(mallHomeGoods.getMenuNo()));
                    goodsInfoVO.setGoodsPrice(MoneyUtil.long2Str(mallHomeGoods.getPrice()));
                    goodsInfoVO.setGoodsImg(mallHomeGoods.getGoodsImage());
                    goodsInfoVO.setSendGoodsMethod(mallHomeGoods.getGoodsSendMethod());
                    goodsInfoVoS.add(goodsInfoVO);
                }
                goodsTableVO.setGoodsList(goodsInfoVoS);
                goodsTableVOList.add(goodsTableVO);
            }
            vo.setDisAreas(goodsTableVOList);
        }

        // 返回填充好的视图对象
        return vo;
    }

    /**
     * 根据商品类别列表获取商品菜单列表
     * 此方法旨在过滤和转换给定的商品类别列表，生成一个不包含“全部”类别的商品菜单列表
     *
     * @param cateList 商品类别列表，用于生成商品菜单
     * @return 返回过滤后的商品菜单列表，不包含“全部”类别的商品信息
     */
    private ArrayList<KingKongConfigInfoVO.GoodsMenu> getGoodsMenus(List<MallHomeGoodsCate> cateList) {
        // 初始化商品菜单列表
        ArrayList<KingKongConfigInfoVO.GoodsMenu> goodsMenus = new ArrayList<>();

        // 遍历商品类别列表
        for (MallHomeGoodsCate cate : cateList) {
            // 跳过类别名称为“全部”的商品类别，因为它们通常代表默认或汇总类别，不符合特定菜单项的要求
            if ("全部".equals(cate.getMenuName())) {
                continue;
            }

            // 创建新的商品菜单对象，用于存储当前类别的相关信息
            KingKongConfigInfoVO.GoodsMenu goodsMenu = new KingKongConfigInfoVO.GoodsMenu();
            // 设置菜单名称，这是当前类别的标识
            goodsMenu.setMenuName(cate.getMenuName());
            // 设置菜单是否置顶的属性，根据类别信息决定菜单的排序或显示优先级
            goodsMenu.setIsSticky(cate.getIsSticky());
            goodsMenu.setIsTop3Sort(cate.getIsTop3Sort());
            // 将构建完成的商品菜单添加到列表中，以便后续处理或返回
            goodsMenus.add(goodsMenu);
        }

        // 返回商品菜单列表，该列表不包含任何名称为“全部”的类别
        return goodsMenus;
    }

    /**
     * 根据类型对不同的配置进行设置
     *
     * @param con      请求参数
     * @param kingKong 配置
     */
    private void byKingKongType(KingKongConfigCondition.AddOrUpdateKingKongConfigCon con, MallKingKongConfig kingKong) {
        if (StringUtils.isBlank(con.getType())) {
            throw new YsException(-1, "请选择类型");
        }
        if (con.getType().equals(MallKingKongConfig.Enums.BIG_BRAND_PURCHASE.name())) {
            QueryWrapper<GoodsBrand> brandQw = new QueryWrapper<>();
            brandQw.lambda().in(GoodsBrand::getId, con.getBrandIdList());
            List<GoodsBrand> brandList = iGoodsBrandService.list(brandQw);
            ArrayList<MallKingKongBrand> mallKingKongBrands = new ArrayList<>();
            Map<Long, GoodsBrand> brandMap = brandList.stream()
                    .collect(Collectors.toMap(GoodsBrand::getId, brand -> brand));
            for (Long l : con.getBrandIdList()) {
                MallKingKongBrand kb = new MallKingKongBrand();
                GoodsBrand goodsBrand = brandMap.get(l);
                if (Objects.isNull(goodsBrand)) {
                    continue;
                }
                kb.setName(goodsBrand.getBrandName());
                kb.setBrandId(l);
                kb.setKingKongId(kingKong.getId());
                mallKingKongBrands.add(kb);
            }

            iMallKingKongBrandService.saveBatch(mallKingKongBrands);
        }
        if (con.getType().equals(MallKingKongConfig.Enums.CASH_ONLY.name())) {
            ArrayList<MallHomeGoodsCate> goodsCateList = new ArrayList<>();
            // 遍历商品分类列表，将每个分类的信息复制到新的 MallHomeGroupGoodsCate 对象中，并设置区域ID，然后添加到待保存的列表中
            for (KingKongConfigCondition.GoodsMenu goodsMenu : con.getGoodsMenus()) {
                MallHomeGoodsCate mallHomeGoodsCate = new MallHomeGoodsCate();
                BeanUtils.copyProperties(goodsMenu, mallHomeGoodsCate);
                mallHomeGoodsCate.setGroupNo(kingKong.getNo());
                mallHomeGoodsCate.setNo("cate" + UUID.randomUUID());
                goodsCateList.add(mallHomeGoodsCate);
            }
            MallHomeGoodsCate mallHomeGoodsCate = new MallHomeGoodsCate();
            mallHomeGoodsCate.setMenuName("全部");
            mallHomeGoodsCate.setGroupNo(kingKong.getNo());
            mallHomeGoodsCate.setIsSticky(EnableEnum.YES.getEnable());
            mallHomeGoodsCate.setNo("cate" + UUID.randomUUID());
            goodsCateList.add(mallHomeGoodsCate);
            // 批量保存商品分类信息
            iMallHomeGoodsCateService.saveBatch(goodsCateList);
            // 将商品分类列表转换为Map，便于后续快速获取分类ID
            Map<String, String> cateMap = goodsCateList.stream().collect(Collectors.toMap(MallHomeGoodsCate::getMenuName, MallHomeGoodsCate::getNo));

            ArrayList<MallHomeGoods> mallHomeGoodsList = new ArrayList<>();
            ArrayList<MallHomeKingKongDisArea> kingKongDisAreas = new ArrayList<>();
            for (KingKongConfigCondition.DisArea disArea : con.getDisAreas()) {
                if (Objects.isNull(disArea)) {
                    continue;
                }
                MallHomeKingKongDisArea kingKongDisArea = new MallHomeKingKongDisArea();
                kingKongDisArea.setAreaName(disArea.getName());
                kingKongDisArea.setGroupNo(kingKong.getNo());
                kingKongDisArea.setNo("dis_area" + UUID.randomUUID());

                List<KingKongConfigCondition.GoodsInfo> goodsList = disArea.getGoodsList();
                if (CollectionUtils.isEmpty(goodsList)) {
                    continue;
                }
                for (KingKongConfigCondition.GoodsInfo goodsInfo : goodsList) {
                    MallHomeGoods goods = new MallHomeGoods();
                    // 设置商品ID
                    goods.setGoodsId(goodsInfo.getGoodsId());
                    // 设置品牌名称
                    goods.setBrandName(goodsInfo.getBrandName());
                    // 设置商品图片URL
                    goods.setGoodsImage(goodsInfo.getGoodsImg());
                    // 设置商品名称
                    goods.setGoodsName(goodsInfo.getGoodsName());
                    // 将字符串价格转换为Long类型并设置
                    goods.setPrice(MoneyUtil.str2Long(goodsInfo.getGoodsPrice()));
                    // 设置优惠折扣
                    goods.setYsCoinDiscount(goodsInfo.getYsCoinDiscount());
                    // 设置商品配送方式
                    goods.setGoodsSendMethod(goodsInfo.getSendGoodsMethod());
                    goods.setIsMainAreaDis(EnableEnum.YES.getEnable());
                    // 设置库存量
                    goods.setStock(goodsInfo.getStock());
                    goods.setDisAreaNo(kingKongDisArea.getNo());
                    String cateNo = cateMap.get(StringUtils.isNotBlank(goodsInfo.getCateName()) ? goodsInfo.getCateName() : "全部");
                    goods.setMenuNo(cateNo);
                    goods.setHomeGroupNo(kingKong.getNo());
                    mallHomeGoodsList.add(goods);
                }
                kingKongDisAreas.add(kingKongDisArea);
            }
            iMallHomeKingKongDisAreaService.saveBatch(kingKongDisAreas);
            iMallHomeGoodsService.saveBatch(mallHomeGoodsList);

        }
    }

    /**
     * 获取品牌商品数量
     *
     * @param brandIdList 品牌id
     * @return 商品数量
     */
    private Integer getBrandGoodsNum(List<Long> brandIdList) {
        if (CollectionUtils.isEmpty(brandIdList)) {
            return 0;
        }
        QueryWrapper<Goods> goodsQw = new QueryWrapper<>();
        goodsQw.lambda().in(Goods::getBrandId, brandIdList);
        goodsQw.lambda().eq(Goods::getIsLine, EnableEnum.YES.getEnable());
        goodsQw.lambda().eq(Goods::getIsDelete, EnableEnum.NO.getEnable());
        return (int) iGoodsService.count(goodsQw);
    }


}
